let showUserData = async (ctx,next) => {
    let ObtainList=await ctx.db.querys(`select userid,username,password,age,sex,phone,roleid,address,entrydate,createtime,name,lastlogin  from user where state='ok'`); 
    ctx.body={code:1,msg:'获取数据列表成功',data:ObtainList};
}
//随机字符串
let  randomString =(len) => {
 　　len = len ||6;
 　　var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
 　　var maxPos = $chars.length;
  　　var pwd = '';
  　　for (let i = 0; i < len; i++) {
  　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
 　　}
　　return pwd;
 } 
 //用户添加
let userAdd = async (ctx,next) => {
    let isOpr = await ctx.isJudge(ctx,'A');

    // 判断当前用户是否可以操作
    if(!isOpr){
        ctx.body = {code:0,msg:'您无权操作'}
        return
    }

    let str = new Date().getTime()+''
    let userid = str.substring(str.length-8)+randomString(6);
    let createtime = new Date().getTime()+'';
    let state = 'ok';
    let {obj} = ctx.request.body;
    let userdata=await ctx.db.querys("select * from user");
	let userIndex=userdata.find(item=>item.username===obj.username);
	if(userIndex) return ctx.body={code:0,msg:"该用户已存在"};
    let obj1 = {...obj,createtime,state,userid};
    let add = await ctx.db.insert('user',obj1);
    if(!add.affectedRows) return ctx.body = {code:0,msg:'添加失败，请稍后再试'}
    return ctx.body={code:1,msg:'用户添加成功'}
}
//用户编辑
let userEdit = async (ctx,next) => {
    let isOpr = await ctx.isJudge(ctx,'E');

    // 判断当前用户是否可以操作
    if(!isOpr){
        ctx.body = {code:0,msg:'您无权操作'}
        return
    }
     let {obj} =ctx.request.body
     console.log(obj)
     let edit = await ctx.db.querys(`update user set state='${obj.state}' , password='${obj.password}', roleid='${obj.roleid}' where userid='${obj.userid}' `)
     if(!edit.affectedRows) return ctx.body = {code:0,msg:'编辑失败，请稍后再试'}
     ctx.body={code:1,msg:'用户编辑成功'}

}
//用户冻结
let  userFreeze = async (ctx,next) => {
    let isOpr = await ctx.isJudge(ctx,'D');

  // 判断当前用户是否可以操作
  if(!isOpr){
      ctx.body = {code:0,msg:'您无权操作'}
      return
  }

  //当前登录用户如果存在删除的权限 则可执行冻结
  const {userid} = ctx.request.body;
  console.log(ctx.request.body)
  let dbReturn = await ctx.db.querys(`update user set state='freeze' where userid='${userid}'`);

  if(!dbReturn.affectedRows){
      ctx.body = {code:0,msg:'操作失败，请重新再来'}
      return
  }
  ctx.body = {code:1,msg:'冻结成功'}
}
//模糊搜索
let userSearch = async (ctx,next) => {
    let {obj} = ctx.request.body
    let arr = await ctx.db.querys(`select * from user where state='ok'`)
    if(obj.username){
         arr = arr.filter(item=>item.username.includes(obj.username))
    } 
    if (obj.type){
        if(!obj.starttime ||!obj.endtime ){
            ctx.body={code:1,msg:'由于没有填写开始时间和结束时间，所以 无法搜索到更精确的数据',data:arr}
            return 
        }
          obj.type === "createtime"? arr = arr.filter(item => item.createtime >obj.starttime&& item.createtime<obj.endtime):arr = arr.filter(item => item.lastlogin>obj.starttime&&item.lastlogin<obj.endtime)
    }
    if (obj.starttime&&!obj.endtime){
        arr = arr.filter(item => item.createtime >obj.starttime && item.lastlogin>obj.starttime)
        ctx.body = {code:1,msg:'查询成功',data:arr}
        return
    }
    if (!obj.starttime&&obj.endtime){
        arr = arr.filter(item => item.createtime <obj.endtime && item.lastlogin<obj.endtime)
        ctx.body = {code:1,msg:'查询成功',data:arr}
        return
    }
    if (obj.starttime&&obj.endtime){
        arr = arr.filter(item=>item.createtime>obj.starttime&&item.createtime<obj.endtime&&item.lastlogin>obj.starttime&&item.lastlogin<obj.endtime)
        ctx.body = {code:1,msg:'查询成功',data:arr}
    }
    if(!arr.length){
        ctx.body = {code:1,msg:'暂无数据',data:[]}
        return
    }
    ctx.body={code:1,msg:'查询成功',data:arr}
}
//黑名单;
let userBlackList = async (ctx,next) => {
    let ObtainList=await ctx.db.querys(`select userid,username,password,age,sex,phone,roleid,address,entrydate,createtime,name,lastlogin  from user where state='freeze'`); 
    ctx.body={code:1,msg:'获取数据列表成功',data:ObtainList};
}
//黑名单搜索；
let userBlackListSearch = async (ctx,next) => {
    let {username} = ctx.request.body
    console.log(ctx.request.body)
    let arr = await ctx.db.querys(`select * from user where state='freeze'`)
   arr = arr.filter(item =>item.username.includes(username));
    ctx.body={code:1,msg:'搜索成功',data:arr}
}

export default {
    showUserData,userAdd,userEdit,userFreeze,userSearch,userBlackList,userBlackListSearch
}